<template>
  <div class="box">
    <el-form
      ref="ruleFormRef"
      style="max-width: 600px"
      :model="ruleForm"
      :rules="rules"
      label-width="auto"
      class="demo-ruleForm"
      :size="formSize"
      status-icon
    >
      <el-form-item label="登录账号" prop="userName">
        <el-input v-model="ruleForm.userName" />
      </el-form-item>
      <el-form-item label="登录密码" prop="userPass">
        <el-input v-model="ruleForm.userPass" />
      </el-form-item>

      <el-form-item label="手机号" required>
      <el-col :span="11">
        <el-form-item prop="userMobile">
        <el-input v-model="ruleForm.userMobile" />
        </el-form-item>
      </el-col>
      <el-col class="text-center" :span="2">
        <span class="text-gray-500"></span>
      </el-col>
      <el-col :span="11">
        <el-form-item>
          <el-button @click="onSendCode">发送验证码</el-button>
        </el-form-item>
      </el-col>
    </el-form-item>


      <el-form-item label="验证码" prop="userCode">
        <el-input v-model="ruleForm.userCode" />
      </el-form-item>
      
      <el-form-item>
        <el-button type="primary" @click="submitForm(ruleFormRef)">
          立刻登录
        </el-button>
        <el-button @click="resetForm(ruleFormRef)">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from "vue";
import { ElMessage, type ComponentSize, type FormInstance, type FormRules } from "element-plus";
import { sendCode,userLogin } from '@/api/api'
import { useRouter } from "vue-router";

interface RuleForm {
  userName: string;
  userPass: string;
  userMobile: string;
  userCode: string;
}

const router = useRouter();
const formSize = ref<ComponentSize>("default");
const ruleFormRef = ref<FormInstance>();
const ruleForm = reactive<RuleForm>({
    userName: "",
    userPass: "",
    userMobile: "",
    userCode: "",
});

const locationOptions = ["Home", "Company", "School"];

const rules = reactive<FormRules<RuleForm>>({
    userName: [
        { required: true, message: "请输入登录账号", trigger: "blur" },
        { min: 3, max: 12, message: "密码必须是3到12个字符", trigger: "blur" },
    ],
    userPass: [
        { required: true, message: "请输入登录密码", trigger: "blur" },
    ],
    userMobile: [
        { required: true, message: "请输入手机号", trigger: "blur" },
        { min: 11, max: 11, message: "手机号必须是11位数字", trigger: "blur" },

    ],
    userCode: [
        { required: true, message: "请输入验证码", trigger: "blur" },
    ],
  
});

// 点击登录按钮
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
        // 表单是合法的
        userLogin(ruleForm).then(res => {
            if(res.data.code == 200) {
                // 存储token
                let token = res.data.data.token;
                let user = res.data.data;
                window.sessionStorage.setItem('token', token);
                window.sessionStorage.setItem('user', JSON.stringify(user));

                // 页面跳转
                router.push('/home');
            }
            else {
                let msg = res.data.msg;
                ElMessage.error(msg);
            }
        })
    } else {
        // 表单不合法
      console.log("error submit!", fields);
    }
  });
};

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
};

const options = Array.from({ length: 10000 }).map((_, idx) => ({
  value: `${idx + 1}`,
  label: `${idx + 1}`,
}));

// 发送验证码
const onSendCode = () => {
    sendCode(ruleForm.userMobile).then(res => {
        if(res.data.code == 200) {
            ElMessage.success('验证码发送成功');
        }
    })
}

</script>

<style scoped></style>
